﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace EWDTAssignment
{
    public partial class EditProfile : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["accountID"] = 2;//to be removed
                if (Session["accountID"] != null)
                {
                    int q = int.Parse(Session["accountID"].ToString());
                    DataTable dt = App_Code.DBManager.displayProfile(q);
                    txtbxFn.Text = dt.Rows[0][5].ToString();
                    txtbxln.Text = dt.Rows[0][6].ToString();
                    string i = (dt.Rows[0][13].ToString());
                    if (i == "F")
                    {
                        RadioButtonList1.SelectedValue = "Female";
                    }
                    else
                    {
                        RadioButtonList1.SelectedValue = "Male";
                    }

                    txtbxdob1.Text = Convert.ToDateTime(dt.Rows[0][12].ToString()).ToShortDateString();
                    txtbxContactNum.Text = dt.Rows[0][8].ToString();
                    txtbxAddress.Text = dt.Rows[0][9].ToString();
                    txtPostalCode.Text = dt.Rows[0][11].ToString();
                    txtbxEmail.Text = dt.Rows[0][10].ToString();
                }
            }
        }

        protected void DisplayAlert(string message)
        {
            ClientScript.RegisterStartupScript(
            this.GetType(),
            Guid.NewGuid().ToString(),
            string.Format("alert('{0}');",
            message.Replace("'", @"\'").Replace("\n", "\\n").Replace("\r", "\\r")),
            true);
        }


        protected void btnSubmitAns_Click(object sender, EventArgs e)
        {
            if (this.IsValid)
            {
                Session["accountID"] = 2;//to be removed
                if (Session["accountID"] != null)
                {

                    string Qns = SecurityQuestionDL.SelectedItem.Text;
                    string Ans = txtbxSecurityAns.Text;
                    int AccountID = int.Parse(Session["accountID"].ToString());
                    App_Code.EditProfile ed = new App_Code.EditProfile(Qns, Ans, AccountID);
                    App_Code.DBManager.UpdateQnA(ed);
                    DisplayAlert("Security question and answer updated.");


                }
            }
        }
        protected void txtbxSubmitPass_Click(object sender, EventArgs e)
        {
            if (this.IsValid)
            {

                int q = int.Parse(Session["accountID"].ToString());
                DataTable dt = App_Code.DBManager.getAccountProfile(q);
                string password = txtbxRPassword.Text;
                int accountID = int.Parse(Session["accountID"].ToString());
                if (txtbxOPassword.Text.Equals(dt.Rows[0][0].ToString()))// update all password
                {
                    App_Code.EditProfile ep = new App_Code.EditProfile(password, accountID);
                    App_Code.DBManager.UpdateProfileAcc(ep);
                    DisplayAlert("Password updated.");
                }
                else
                {

                    DisplayAlert("Worng old password entered. Please try again");
                }

            }
        }


        protected void btnSubmitp_Click(object sender, EventArgs e)
        {
            if (this.IsValid)
            {
                int AccountID = int.Parse(Session["accountID"].ToString());
                string FirstName = txtbxFn.Text;
                string LastName = txtbxln.Text;
                String Gender = " ";
                if (RadioButtonList1.SelectedValue.Equals("f"))
                {
                    Gender = "F";
                }

                else
                {
                    Gender = "F";
                }
                DateTime DOB = Convert.ToDateTime(txtbxdob1.Text.ToString());
                int ContactNum = Convert.ToInt32(txtbxContactNum.Text.ToString());
                string Address = txtbxAddress.Text;
                int PostalCode = Convert.ToInt32(txtPostalCode.Text.ToString());
                string Email = txtbxEmail.Text;
                App_Code.EditProfile ep = new App_Code.EditProfile(AccountID, FirstName, LastName, Gender, DOB, ContactNum, Address, PostalCode, Email);
                App_Code.DBManager.UpdateProfileCust(ep);
                DisplayAlert("Profile Updated.");
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {

        }
    }
}